// #include <REGtenxTM52F1376.h>
// #include "TM52F1376_bsp.h"
#include "ad_temp.h"
#include "var.h"
#include "TimeEvent.h"
#include "math.h"
#include "display.h"

uint8_t xdata chan;

/************* NTC B值表***********/
uint16_t code NTCTAB0[NtcTableNum] = // NtcTableNum
	{
		0xBF5, //-50℃
		0xBE7, //-49℃
		0xBD8, //-48℃
		0xBC8, //-47℃
		0xBB8, //-46℃
		0xBA7, //-45℃
		0xB96, //-44℃
		0xB84, //-43℃
		0xB71, //-42℃
		0xB5D, //-41℃
		0xB49, //-40℃
		0xB34, //-39℃
		0xB1E, //-38℃
		0xB07, //-37℃
		0xAF0, //-36℃
		0xAD8, //-35℃
		0xABF, //-34℃
		0xAA5, //-33℃
		0xA8B, //-32℃
		0xA70, //-31℃
		0xA54, //-30℃
		0xA38, //-29℃
		0xA1B, //-28℃
		0x9FD, //-27℃
		0x9DF, //-26℃
		0x9C0, //-25℃
		0x9A0, //-24℃
		0x980, //-23℃
		0x95F, //-22℃
		0x93E, //-21℃
		0x91C, //-20℃
		0x8FA, //-19℃
		0x8D7, //-18℃
		0x8B4, //-17℃
		0x890, //-16℃
		0x86D, //-15℃
		0x848, //-14℃
		0x824, //-13℃
		0x800, //-12℃
		0x7DB, //-11℃
		0x7B6, //-10℃
		0x791, //-9℃
		0x76C, //-8℃
		0x747, //-7℃
		0x722, //-6℃
		0x6FD, //-5℃
		0x6D8, //-4℃
		0x6B4, //-3℃
		0x68F, //-2℃
		0x66B, //-1℃
		0x647, // 0℃
		0x623, // 1℃
		0x5FF, // 2℃
		0x5DC, // 3℃
		0x5B9, // 4℃
		0x597, // 5℃
		0x575, // 6℃
		0x553, // 7℃
		0x532, // 8℃
		0x511, // 9℃
		0x4F1, // 10℃
		0x4D1, // 11℃
		0x4B2, // 12℃
		0x493, // 13℃
		0x475, // 14℃
		0x457, // 15℃
		0x43A, // 16℃
		0x41E, // 17℃
		0x402, // 18℃
		0x3E6, // 19℃
		0x3CC, // 20℃
		0x3B1, // 21℃
		0x398, // 22℃
		0x37F, // 23℃
		0x366, // 24℃
		0x34E, // 25℃
		0x337, // 26℃
		0x320, // 27℃
		0x30A, // 28℃
		0x2F4, // 29℃
		0x2DF, // 30℃
		0x2CA, // 31℃
		0x2B6, // 32℃
		0x2A3, // 33℃
		0x290, // 34℃
		0x27D, // 35℃
		0x26B, // 36℃
		0x25A, // 37℃
		0x249, // 38℃
		0x238, // 39℃
		0x228, // 40℃
		0x218, // 41℃
		0x209, // 42℃
		0x1FA, // 43℃
		0x1EC, // 44℃
		0x1DE, // 45℃
		0x1D0, // 46℃
		0x1C3, // 47℃
		0x1B6, // 48℃
		0x1AA, // 49℃
		0x19E, // 50℃
		0x192, // 51℃
		0x187, // 52℃
		0x17C, // 53℃
		0x171, // 54℃
		0x167, // 55℃
		0x15D, // 56℃
		0x153, // 57℃
		0x14A, // 58℃
		0x140, // 59℃
		0x138, // 60℃
		0x12F, // 61℃
		0x127, // 62℃
		0x11E, // 63℃
		0x116, // 64℃
		0x10F, // 65℃
		0x107, // 66℃
		0x100, // 67℃
		0xF9,  // 68℃
		0xF2,  // 69℃
		0xEC,  // 70℃
		0xE5,  // 71℃
		0xDF,  // 72℃
		0xD9,  // 73℃
		0xD3,  // 74℃
		0xCE,  // 75℃
		0xC8,  // 76℃
		0xC3,  // 77℃
		0xBE,  // 78℃
		0xB9,  // 79℃
		0xB4,  // 80℃
		0xAF,  // 81℃
		0xAB,  // 82℃
		0xA6,  // 83℃
		0xA2,  // 84℃
		0x9E,  // 85℃
		0x9A,  // 86℃
		0x96,  // 87℃
		0x92,  // 88℃
		0x8E,  // 89℃
		0x8A,  // 90℃
		0x87,  // 91℃
		0x84,  // 92℃
		0x80,  // 93℃
		0x7D,  // 94℃
		0x7A,  // 95℃
		0x77,  // 96℃
		0x74,  // 97℃
		0x71,  // 98℃
		0x6E,  // 99℃
		0x6C   // 100℃
};

/*********************************************************************
 * @fn   void Adc_Temp_Config( void )
 *
 * @brief  adc 配置
 *
 * @param  none
 *
 * @return none
 *
 */
void Adc_Temp_Config(void)
{
	// IO???��?ADC??
	PORTIDX = PORT0;
	PINMOD54 = PINMOD54 & 0x00 | PIN_L_MODE_ADC | PIN_H_MODE_ADC;

	// temp = OPTION & ADCKS_Mask;
	//??????????
	OPTION = OPTION & ADCKS_Mask | ADCKS_sys_32; // adc???
	SET_ADVREFS_00;								 // Vcc?��????

	// INTE1 = (INTE1 & ~INTE1_ADIE_MASK) | 1 << INTE1_ADIE_POS; // 1:???? ADC ?��?

	// ADCHS = (ADCHS & ~ADCHS_ADCHS_MASK); // ADC CH0  P0.4
	// ADCHS = (ADCHS & ~ADCHS_ADCHS_MASK) | 0x01; // ADC CH1 	P0.5
}

/*********************************************************************
 * @fn     uint16_t adc_GetValue(uint8_t ch)
 *
 * @brief  ???????????AD?
 *
 * @param  none
 *
 * @return none
 *
 */
uint16_t adc_GetValue(uint8_t ch)
{
	NTC_ADValue_Sum = 0;
	ADCHS = (ADCHS & ~ADCHS_ADCHS_MASK) | ch << ADCHS_ADCHS_POS;
	for (i = 0; i < 5; i++)
	{
		ADSOC = 1;
		while (ADSOC)
			;
		templ = ADCDL;
		temph = ADCDH;

		NTC_ADValue = (temph << 4) & 0xff0;
		NTC_ADValue |= (templ >> 4);

		NTC_ADValue_Sum = NTC_ADValue_Sum + NTC_ADValue;
	}

	NTC_ADValue = NTC_ADValue_Sum / 5;

	return NTC_ADValue;
}

/***************************************************************************************************
 * @fn      void DataCmdHandler ( char *pBuf )
 *
 * @brief
 *
 * @param   None
 *
 * @return  None
 */
uint16_t look_up_Temp_table(uint16_t *a, uint16_t ArrayLong, uint16_t Arraydata)
{
	uint16_t idata begin, end, middle;
	uint16_t idata i = 0;

	int16_t idata k = 0;
	uint32_t idata b = 0;
	uint16_t idata Y3 = 0;

	int16_t idata c = 0;

	begin = 0;
	end = ArrayLong - 1;

	if (Arraydata <= a[end])
	{
		Y3 = end * 10;
		return Y3;
	}
	else if (Arraydata >= a[begin])
	{
		Y3 = begin * 10;
		return Y3;
	}

	while (begin < end)
	{
		middle = (begin + end) / 2;
		if (Arraydata == a[middle])
		{
			Y3 = middle * 10;
			break;
		}

		if (a[middle + 1] < Arraydata && Arraydata < a[middle])
		{
			k = (a[middle + 1] - a[middle]);
			k = 100 / k;
			b = 100 * middle - k * a[middle];
			//			c = k * Arraydata;
			c = k * Arraydata + b;
			Y3 = c / 10;
			break;
		}

		if (Arraydata > a[middle])
			end = middle;
		else if (Arraydata < a[middle])
			begin = middle;

		if (i++ > ArrayLong)
			break;
	}
	if (begin > end)
		return 0;

	return Y3;
}

/*********************************************************************
 * @fn    void NTC_ErrCode_Task( uint8_t num,uint8_t chan ) // 200ms
 *
 * @brief  NTC故障检测
 *
 * @param  none
 *
 * @return none
 *
 */
void NTC_ErrCode_Task(uint8_t chan) // 200ms
{
	if (ntcPara[chan].AdValue > NtcValueMax)
	{
		if (ntcPara[chan].OpenCirErrCount < 3)
			ntcPara[chan].OpenCirErrCount++;
		else
		{
			if (ntcPara[chan].OpenCirErr == 0)
			{
				ntcPara[chan].OpenCirErr = 1;
				if (chan == 0) // NTC0
				{
					ErrorState.OneBit.Ntc0Error |= 0x01;

					displayState = 0;
					// Set_Event(&timer_10msDB[DisplayRefurbish_TaskId], DisplayRefurbish);
					tPara.sensorErrCompStopTimeCount = 0;
					tPara.sensorErrCompRunTimeCount = 0;
					Flag2.OneBit.SensorErr = 1;

					DefrostState = defrostingCycle;
					tPara.defrosteCycleTimeCount = 0;
					tPara.defrosteTimeCount = 0;

					Flag3.OneBit.DisplayState = Real;
				}
				if (chan == 1) // NTC1
				{
					ErrorState.OneBit.Ntc1Error |= 0x01;
				}
			}
		}
	}
	else
	{
		if (ntcPara[chan].OpenCirErr)
		{
			ntcPara[chan].OpenCirErrCount--;
			if (ntcPara[chan].OpenCirErrCount == 0)
			{
				ntcPara[chan].OpenCirErr = 0;
				ntcPara[chan].ADFirstDect = 0;
				if (chan == 0) // NTC0
				{
					ErrorState.OneBit.Ntc0Error &= 0x02;
					Flag3.OneBit.DisplayState = Real;

					// TempInside = ntcPara[chan].Temp_Average;
					// TargetTemp = TempInside;
					// DisplayTemp = TempInside;

					tPara.sensorErrCompStopTimeCount = 0;
					tPara.sensorErrCompRunTimeCount = 0;
					Flag2.OneBit.SensorErr = 1;
				}
				if (chan == 1) // NTC1
				{
					ErrorState.OneBit.Ntc1Error &= 0x02;
				}
			}
		}
		else
		{
			ntcPara[chan].OpenCirErr = 0;
			ntcPara[chan].OpenCirErrCount = 0;
		}
	}
	/**********/
	if (ntcPara[chan].AdValue < NtcValueMin)
	{
		if (ntcPara[chan].ShortCirErrCount < 3)
			ntcPara[chan].ShortCirErrCount++;
		else
		{
			if (ntcPara[chan].ShortCirErr == 0)
			{
				ntcPara[chan].ShortCirErr = 1;
				if (chan == 0)
				{
					ErrorState.OneBit.Ntc0Error |= 0x02;

					displayState = 0;
					// Set_Event(&timer_10msDB[DisplayRefurbish_TaskId], DisplayRefurbish);
					tPara.sensorErrCompStopTimeCount = 0;
					tPara.sensorErrCompRunTimeCount = 0;
					Flag2.OneBit.SensorErr = 1;

					DefrostState = defrostingCycle;
					tPara.defrosteCycleTimeCount = 0;
					tPara.defrosteTimeCount = 0;

					Flag3.OneBit.DisplayState = Real;
				}
				if (chan == 1)
				{
					ErrorState.OneBit.Ntc1Error |= 0x02;
				}
			}
		}
	}
	else
	{
		if (ntcPara[chan].ShortCirErr)
		{
			ntcPara[chan].ShortCirErrCount--;
			if (ntcPara[chan].ShortCirErrCount == 0)
			{
				ntcPara[chan].ShortCirErr = 0;
				ntcPara[chan].ADFirstDect = 0;
				if (chan == 0)
				{
					ErrorState.OneBit.Ntc0Error &= 0x01;

					//					Flag2.OneBit.ntc0FirstDect = 0;
					Flag3.OneBit.DisplayState = Real;
					TempInside = ntcPara[chan].Temp_Average;
					TargetTemp = TempInside;
					DisplayTemp = TempInside;

					tPara.sensorErrCompStopTimeCount = 0;
					tPara.sensorErrCompRunTimeCount = 0;
					Flag2.OneBit.SensorErr = 1;
				}
				if (chan == 1)
				{
					ErrorState.OneBit.Ntc1Error &= 0x01;
				}
			}
		}
		else
		{
			ntcPara[chan].ShortCirErr = 0;
			ntcPara[chan].ShortCirErrCount = 0;
		}
	}
}

/*********************************************************************
 * @fn    void Adc_Ntc_Evt( void )
 *
 * @brief  NTC温度检测
 *
 * @param  none
 *
 * @return none
 *
 */
void Adc_Ntc_Evt(void) // 200ms
{
	timer_10msDB[NtcEvt_TaskId].Flag = 0;
	timer_10msDB[NtcEvt_TaskId].Enable = TRUE;
	timer_10msDB[NtcEvt_TaskId].Time = tempTaskCycle; // 100ms

	ntcPara[0].AdValue = adc_GetValue(0);
	ntcPara[1].AdValue = adc_GetValue(1);

	NTC_ErrCode_Task(0);
	NTC_ErrCode_Task(1);

	ntcPara[0].Temp_Average = look_up_Temp_table(NTCTAB0, NtcTableNum, ntcPara[0].AdValue) - NtcOffset + cfgPara[CoolingMode].C0;
	ntcPara[1].Temp_Average = look_up_Temp_table(NTCTAB0, NtcTableNum, ntcPara[1].AdValue) - NtcOffset + cfgPara[CoolingMode].C1;

	for (chan = 0; chan < 2; chan++)
	{
		if (ntcPara[chan].ADFirstDect == 0)
		{
			ntcPara[chan].ADFirstDect = 1;
			for (i = 0; i < 5; i++)
			{
				ntcPara[chan].Temp_Buff[i] = ntcPara[chan].Temp_Average;
			}
			// ntcPara[chan].Temp_Average = ntcPara[chan].Temp_Average;

			if (chan == 0) // ntc0
			{
				TempInside = ntcPara[0].Temp_Average;
				TargetTemp = TempInside;
				DisplayTemp = TempInside;
			}
		}
		else
		{
			NTC_ADValue_Sum = 0;
			for (i = 0; i < 4; i++)
			{
				ntcPara[chan].Temp_Buff[i] = ntcPara[chan].Temp_Buff[i + 1];
				NTC_ADValue_Sum += ntcPara[chan].Temp_Buff[i];
			}
			ntcPara[chan].Temp_Buff[4] = ntcPara[chan].Temp_Average;
			NTC_ADValue_Sum += ntcPara[chan].Temp_Buff[4];
			ntcPara[chan].Temp_Average = NTC_ADValue_Sum / 5;
		}
	}

	/*************************************************************************************************************************************************/
	if (ErrorState.OneBit.Ntc0Error == 0) //控温传感器正常
	{
		CtrTargetTemp = cfgPara[CoolingMode].St * 10;
		if (ntcPara[0].OpenCirErrCount == 0 && ntcPara[0].ShortCirErrCount == 0)
		{
			TempInside = ntcPara[0].Temp_Average;
		}

		//上电延时报警
		if (Flag2.OneBit.AlarmPowerOnDelay == 1)
		{
			/************** 高温报警 **************/
			if (cfgPara[CoolingMode].AH != 0 && Flag2.OneBit.Alarmd8Delay != 1 && DefrostState == defrostingCycle) // DefrostState == defrostingCycle &&
			{
				if (TempInside >= (CtrTargetTemp + cfgPara[CoolingMode].AH * 10)) //  TempInside
				{
					if (ErrorState.OneBit.AH == 0)
					{
						tPara.AH_Count++;
						if ((tPara.AH_Count * tempTaskCycle) > (cfgPara[CoolingMode].Ad * tMinute))
						{
							ErrorState.OneBit.AH = 1;
							// Set_Event(&timer_10msDB[DisplayRefurbish_TaskId], DisplayRefurbish);
						}
					}
				}
				else
				{
					tPara.AH_Count = 0;
					ErrorState.OneBit.AH = 0;
				}
			}
			else
			{
				tPara.AH_Count = 0;
				ErrorState.OneBit.AH = 0;
			}

			/************** 低温报警 **************/
			if (cfgPara[CoolingMode].AL != 0) // DefrostState == defrostingCycle &&
			{
				if (TempInside <= (CtrTargetTemp - cfgPara[CoolingMode].AL * 10)) //  TempInside
				{
					if (ErrorState.OneBit.AL == 0)
					{
						tPara.AL_Count++;
						if ((tPara.AL_Count * tempTaskCycle )> (cfgPara[CoolingMode].Ad * tMinute))
						{
							ErrorState.OneBit.AL = 1;
							// Set_Event(&timer_10msDB[DisplayRefurbish_TaskId], DisplayRefurbish);
						}
					}
				}
				else
				{
					tPara.AL_Count = 0;
					ErrorState.OneBit.AL = 0;
				}
			}
			else
			{
				tPara.AL_Count = 0;
				ErrorState.OneBit.AL = 0;
			}
		}

		/*************************************************************************************************************************/

		if (cfgPara[CoolingMode].DP == 0) //实际测量值显示
		{
			tPara.Count2s = 0;
			tPara.Count30m = 0;
			Flag3.OneBit.DisplayState = Real;
			TargetTemp = TempInside;
		}
		else
		{
			CtrTargetTemp = cfgPara[CoolingMode].St * 10;
			CtrTargetTemp1 = cfgPara[CoolingMode].St * 10 + 40;
			CtrTargetTemp2 = cfgPara[CoolingMode].St * 10 - 100;
			if (Flag3.OneBit.DisplayState == Real)
			{
				TargetTemp = TempInside;
				if ((TempInside <= CtrTargetTemp1) && (TempInside >= CtrTargetTemp2))
				{
					tPara.Count2s++;
					if (tPara.Count2s * tempTaskCycle >= 2 * Second)
					{
						tPara.Count2s = 0;
						tPara.Count30m = 0;
						Flag3.OneBit.DisplayState = Amend;

						TargetTemp = CtrTargetTemp + (TempInside - CtrTargetTemp) / 5;
					}
				}
				else
				{
					tPara.Count2s = 0;
				}
			}
			else if (Flag3.OneBit.DisplayState == Amend)
			{
				TargetTemp = CtrTargetTemp + (TempInside - CtrTargetTemp) / 5;

				if (abs(TempInside - CtrTargetTemp) >= amendTemp)
				{
					tPara.Count30m++;
					if ((tPara.Count30m * tempTaskCycle) > (amendTimeMax * Minute))
					{
						tPara.Count30m = 0;
						tPara.Count2s = 0;
						Flag3.OneBit.DisplayState = Real;
						TargetTemp = TempInside;
					}
				}
				else
				{
					tPara.Count30m = 0;
				}
			}
		}
	}
	else
	{
		ErrorState.OneBit.AH = 0;
		ErrorState.OneBit.AL = 0;
		tPara.AH_Count = 0;
		tPara.AL_Count = 0;
	}

	if (ErrorState.OneBit.Ntc1Error == 0)
	{
		if (ntcPara[1].OpenCirErrCount == 0 && ntcPara[1].ShortCirErrCount == 0)
		{
			DefrostTemp = ntcPara[1].Temp_Average;
		}
	}
}
